/*
 * Created by SharpDevelop.
 * User: tjog
 * Date: 10/10/2009
 * Time: 9:09 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


public class Project {
	public Project() {
		dataSet = new DataSet("Data");
		name = "Untitled";
	}
	public DataSet dataSet;
	public string name;
}



public partial class DataViewForm : System.Windows.Forms.Form
{
	private System.Windows.Forms.MainMenu mainMenu;
	private System.Windows.Forms.MenuItem fileMenu;
	//private System.Windows.Forms.MenuItem exitFileMenu;
	private System.Windows.Forms.DataGrid dataGrid;
	private Project project;
	private DataSet myDataSet;
	private DataGridView gridView;// = new DataGridView();
	private StatusStrip statusStrip;
	private ToolStripProgressBar toolStripProgressBar;
	private ToolStripStatusLabel toolStripStatusLabel;
	private BackgroundWorker backgroundWorker;
	
	public DataViewForm()
	{
		InitializeComponent();
		
		backgroundWorker = new BackgroundWorker();
		backgroundWorker.WorkerReportsProgress = true;
		backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
		backgroundWorker.ProgressChanged += new ProgressChangedEventHandler( backgroundWorker1_ProgressChanged);
		backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler( backgroundWorker1_RunWorkerCompleted);
	}
	
	private void InitializeComponent() {
		
		//FlowLayoutPanel flowLayout = new FlowLayoutPanel();
		//flowLayout.FlowDirection = FlowDirection.TopDown;
		//flowLayout.Dock = DockStyle.Fill;
		//this.Controls.Add(flowLayout);
		
		this.myDataSet = new DataSet("MyDataSet");
		this.project = new Project();
		//this.dataGrid = new DataGrid();
		this.gridView = new DataGridView();
		this.gridView.Dock = DockStyle.Fill;

		initMenu();
		
		//this.AutoScaleBaseSize = new System.Drawing.Size(5, 3);
		this.ClientSize = new System.Drawing.Size( 300, 200);

		this.Name = "testStdfFile";
		this.Text = "testStdfFile";
		CenterToScreen();
		
		this.Controls.Add(gridView);
		//flowLayout.Controls.Add(this.gridView);

		//this.Controls.
		this.gridView.Name = "GridView";
		
		this.statusStrip = new System.Windows.Forms.StatusStrip();
		this.statusStrip.Location = new System.Drawing.Point( 0, 251);
		this.statusStrip.TabIndex = 0;
		this.statusStrip.Name = "statusStrip";
		this.statusStrip.Text = "statusStrip";
		this.Controls.Add(this.statusStrip);
		this.toolStripProgressBar = new ToolStripProgressBar();
		toolStripProgressBar.Enabled = false;
		this.toolStripStatusLabel = new ToolStripStatusLabel();
		this.statusStrip.Items.Add(this.toolStripProgressBar);
		this.statusStrip.Items.Add(this.toolStripStatusLabel);
	}
	private void initMenu()
	{
		this.mainMenu = new MainMenu();
		this.fileMenu = new MenuItem("&File");
		MenuItem openFileMenu = new MenuItem("&Open");
		MenuItem exitFileMenu = new MenuItem("E&xit");
		
		this.mainMenu.MenuItems.AddRange( new System.Windows.Forms.MenuItem[] { this.fileMenu });
		this.fileMenu.Index = 0;
		MenuItem menuSeparator = new MenuItem("-");
		this.fileMenu.MenuItems.Add(openFileMenu);
		this.fileMenu.MenuItems.Add(menuSeparator);
		this.fileMenu.MenuItems.Add(exitFileMenu);
		//this.fileMenu.MenuItems.AddRange( new System.Windows.Forms.MenuItem[] { this.exitFileMenu });
	
		exitFileMenu.Click += new System.EventHandler(this.exitFileMenu_Click);
		openFileMenu.Click += new System.EventHandler(this.openFileMenu_Click);
		this.Menu = this.mainMenu;
	}
	
	[STAThread]
	public static void Main()
	{
		Application.EnableVisualStyles();
		Application.Run(new DataViewForm());
	}
	private void exitFileMenu_Click(object sender, System.EventArgs e)
	{
		Application.Exit();
	}
	private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        // This method will run on a thread other than the UI thread.
        // Be sure not to manipulate any Windows Forms controls created
        // on the UI thread from this method.
        string fileName = (string) e.Argument;
        
        backgroundWorker.ReportProgress(0, "Working...");
        
        DataTable newTable = new DataTable(fileName);
		DataFileReader.loadSTDF(fileName, ref newTable);
		this.myDataSet.Tables.Add(newTable);

        //System.Threading.Thread.Sleep(5);
        
        backgroundWorker.ReportProgress( 100, "Complete!");
	}
	private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        this.toolStripProgressBar.Value = e.ProgressPercentage;
        //this.s.Text = e.UserState as String;
        this.toolStripStatusLabel.Text = e.UserState as String;
    }
	private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        toolStripProgressBar.Enabled = false;
        //requestedCountControl.Enabled = true;
        //goButton.Enabled = true;
        
        /// Assign DataTable to DataGridView
        if(this.myDataSet.Tables.Count > 0) {
        	DataTable newTable = this.myDataSet.Tables[this.myDataSet.Tables.Count - 1];
			this.gridView.DataSource = newTable;
			
			/// Set DataGridView headers
			foreach (DataColumn dc in newTable.Columns) {
				gridView.Columns[dc.ColumnName].HeaderText = dc.Caption;
			}
			/// Resize the DataGridView columns to fit the newly loaded content
			gridView.AutoResizeColumns( DataGridViewAutoSizeColumnsMode.AllCells);
			//PrintTable(newTable);
        }


    }
	private void openFileMenu_Click(object sender, System.EventArgs e)
	{
		OpenFileDialog dlg = new OpenFileDialog();
		if(dlg.ShowDialog() == DialogResult.OK) {
			// Do something
			string fileName = dlg.FileName;
			backgroundWorker.RunWorkerAsync(fileName);
						

			//this.dataGrid.DataSource = this.myDataSet;
			//this.dataGrid.DataMember = dlg.FileName;
			//this.dataGrid.SetDataBinding( this.myDataSet, "DataSet");
			
			//newTable.Clear();
			
			//newTable = GetTable();
			

			

		}
	}
    private static void PrintTable(DataTable dt) {
        DataTableReader dtReader = dt.CreateDataReader();
        while (dtReader.Read()) {
            for (int i = 0; i < dtReader.FieldCount; i++) {
                Console.Write("{0} = {1} ",
                    dtReader.GetName(i).Trim(),
                    dtReader.GetValue(i).ToString().Trim());
            }
            Console.WriteLine();
        }
        dtReader.Close();
    }
	    /// <summary>
    /// This example method generates a DataTable.
    /// </summary>
    public static DataTable GetTable()
    {
        //
        // Here we create a DataTable with four columns.
        //
        DataTable table = new DataTable();
        table.Columns.Add("Weight", typeof(int));
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("Breed", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));

        //
        // Here we add unsorted data to the DataTable and return.
        //
        table.Rows.Add(57, "Koko", "Shar Pei", DateTime.Now);
        table.Rows.Add(130, "Fido", "Bullmastiff", DateTime.Now);
        table.Rows.Add(92, "Alex", "Anatolian Shepherd Dog", DateTime.Now);
        table.Rows.Add(25, "Charles", "Cavalier King Charles Spaniel", DateTime.Now);
        table.Rows.Add(7, "Candy", "Yorkshire Terrier", DateTime.Now);
        return table;
    }
}